<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理员工具</title>
    <link rel="stylesheet" href="${ctxPath}/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${ctxPath}/assets/module/admin.css"/>
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-jsonview/1.2.3/jquery.jsonview.css">
</head>

<body>
<% include("../layout/loading.html"){} %>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <div class="layui-col-xs6">
                    <div class="layui-row">
                        <div class="layui-form-item">
                            <label class="layui-form-label">stringRediskey</label>
                            <div class="layui-inline">
                                <input id="rediskey" placeholder="请输入Rediskey" onchange="rediskeyChange()" style="width: 300px;" type="text" class="layui-input"/>
                            </div>
                            <div class="layui-inline">
                                <button id="btnClearCache" class="layui-btn icon-btn"><i class="layui-icon">&#xe640;</i>清除缓存</button>
                            </div>

                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-form-item">
                            <label class="layui-form-label">RedisValue</label>
                            <div class="layui-input-block">
                                <pre class="layui-code" id="redisView"></pre>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-col-xs6">

                </div>
            </div>
        </div>
    </div>

</div>

<!-- js部分 -->
<script type="text/javascript" src="${ctxPath}/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/js/common.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-jsonview/1.2.3/jquery.jsonview.js"></script>
<script>
    layui.use(['layer', 'form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;

        function initRedisJsonView(v) {
            if (isJSON(v)) {
                $("#redisView").JSONView(v);
            } else {
                $("#redisView").html(v);
            }
        };

        function isJSON(str) {
            if (typeof str == 'string') {
                try {
                    var obj=JSON.parse(str);
                    if(typeof obj == 'object' && obj ){
                        return true;
                    }else{
                        return false;
                    }
                } catch(e) {
                    return false;
                }
            }
        }


        window.rediskeyChange = function(){
            var key = $("#rediskey").val();
            if (key){
                $.get("/adminTool/searchStringRedisKey", {key:key}, function (res) {
                    if (res.code == 200) {
                        console.log(res)
                        initRedisJsonView(res.data)
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            }
        }

        $('#btnClearCache').click(function () {
            var key = $("#rediskey").val();
            top.layer.confirm('确定要清除该缓存吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                top.layer.close(i);
                layer.load(2);
                $.post('/adminTool/delStringRedisKey', {
                    key: key,
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        rediskeyChange();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        });

    });
</script>

</body>
</html>